﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Reflection;
using ZhimeiSoft.Framework.Common.Enums;
using ZhimeiSoft.Framework.Common.RefectUtils;
using ZhimeiSoft.Framework.Connector.Interface;


namespace ZhimeiSoft.Framework.Client.Connector.Manager
{
    public class ConnectorManager
    {
        /// <summary>
        /// 在整个应用中共享的框架后台服务接口
        /// </summary>
        private static IConnector connector;

        /// <summary>
        /// 在整个应用中共享的框架后台服务接口
        /// </summary>
        public static IConnector Instance
        {
            get { return connector; }
            set { connector = value; }
        }

        /// <summary>
        /// 是否创建了实例
        /// </summary>
        public static bool HasInstance
        {
            get { return connector != null; }
        }

        public static void CreateConnector(AccessModeEnum accessMode, string serviceUrl)
        {
            Assembly assembly = typeof(IConnector).Assembly;
            try
            {
                switch (accessMode)
                {
                    case AccessModeEnum.emWebService:
                        connector = (IConnector)ReflectHelper.Instance.CreateObject(assembly,
                            @"ZhimeiSoft.Framework.Client.Connector.Proxy.WebService",
                            @"ZhimeiSoft.Framework.Client.Connector.Proxy.WebService.WebServiceConnectorProxy",
                            new object[] { });
                        break;
                    default:
                        connector/*if (this.plContent1.Tag == sender)
                return;

            if (this.currentContent != null)
            {
                this.plContent1.Controls.Remove(this.currentContent);
            }
            this.currentContent = control;
            this.currentContent.Dock = DockStyle.Fill;
            this.plContent1.Controls.Add(this.currentContent);
            this.plContent1.Tag = sender;*/ = (IConnector)ReflectHelper.Instance.CreateObject(assembly,
                            @"ZhimeiSoft.Framework.Client.Connector.Proxy.Local",
                            @"ZhimeiSoft.Framework.Client.Connector.Proxy.Local.LocalConnectorProxy",
                            new object[] { });
                        break;
                }
            }
            catch (Exception ex)
            {
                throw new Exception("连接Connector服务出错", ex);
            }
        }
    }
}
